package com.example.homework;

import android.content.ContentValues;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.homework.model.Contact;

public class CreateContactActivity extends AppCompatActivity {
    private EditText firstNameEdit;
    private EditText lastNameEdit;
    private EditText phoneEdit;
    private Button cancelButton;
    private Button createButton;
    private DBHelper dbHelper;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.create_contact_layout);
        this.firstNameEdit = findViewById(R.id.firstname_edit);
        this.lastNameEdit = findViewById(R.id.lastname_edit);
        this.phoneEdit = findViewById(R.id.phone_edit);
        this.cancelButton = findViewById(R.id.cancel_button);
        this.createButton = findViewById(R.id.create_button);
        this.dbHelper=new DBHelper(this);
        setTitle("创建联系人");

        this.cancelButton.setOnClickListener(v->{
            finish();
        });
        this.createButton.setOnClickListener(v->{
            String firstname=firstNameEdit.getText().toString();
            String lastname=lastNameEdit.getText().toString();
            String phone=phoneEdit.getText().toString();
            Contact contact=new Contact(0,firstname,lastname,phone,false);
            dbHelper.insert(dbHelper.getWritableDatabase(),contact);
            finish();
        });
    }
}
